tempPath = Dir.pwd
require "#{tempPath}/xmlTag.rb"

# TODO: Add Namespace

# file constants
class XMLStringManagerConstants
  OPENNEWFILE = 0
  USESTRING = 1
end

# class that handles all xml strings
class XMLStringManager

  attr_accessor :xmlString
  attr_accessor :xmlTree
  attr_accessor :processedXMLString

  def initialize(xmlSource, fileOrString)

    # open file and add source to string
    if(XMLStringManagerConstants::OPENNEWFILE == xmlSource)

      # if file exists, process it or throw error
      if File.exists?(fileOrString)
        file = File.open(fileOrString, "r")
        @xmlString = file.read
        file.close
      else
        puts "ERROR: File does not exist"
      end

    # use string passed as xml string
    elsif(XMLStringManagerConstants::USESTRING == xmlSource)

      @xmlString = xmlSource

    # bad value passed, alert user
    else

      puts "ERROR: Wrong File Type Selection"

    end

    @processedXMLString = @xmlString

    stripXMLHeader

  end

  def processXMLString
    @xmlTree = XMLDocument.new(@processedXMLString)
  end

  def printXMLTree
    if @xmlTree
      @xmlTree.printXMLData
    end
  end

  # this will change original string
  # consumes header
  def stripXMLHeader

    start = @processedXMLString.index "<?"
    stop = @processedXMLString.index "?>\n"

    if start != nil and stop != nil
      @processedXMLString.slice!(start..(stop + 2))
    end

  end

  def deleteTag(tagName)

    finalStartDeletion = 0
    stopCount = 0
    tagList = tagName.split "/"

    # get tag starting position
    tagList.each { |tagElement|
      formattedTag = "<" + tagElement + ">"

      startDeletion = @processedXMLString[finalStartDeletion..(@processedXMLString.length - 1)].index formattedTag

      # look for complete match
      if nil == startDeletion
        formattedTag = "<" + tagElement + " "
        startDeletion = @processedXMLString[finalStartDeletion..(@processedXMLString.length - 1)].index formattedTag
      end
      
      if startDeletion == nil
        startDeletion = 0
      end
      finalStartDeletion = finalStartDeletion + startDeletion
    }

    openBracketList = @processedXMLString[finalStartDeletion..(@processedXMLString.length - 1)].split "<"

    finalEndDeletion = finalStartDeletion
    openBracketList.each { |element|
      tempStart = finalEndDeletion
      if !element.empty?
        # look to see if this is an end tag
        if element[0] == 47
          finalEndDeletion = finalEndDeletion + element.length
          stopCount = stopCount - 1
        else
          # filter out '/' on seconde to last character
          if element[element.length - 2] == 47 
            # filter out '>' on last character "/>" for an attribute only tag
            if element[element.length - 1] != 62
              stopCount = stopCount + 1
            end
          else
            stopCount = stopCount + 1
          end
          finalEndDeletion = finalEndDeletion + element.length
        end

        puts stopCount
        if 0 == stopCount
          break
        else
          puts element
        end

        # takes care of the '<' that gets taken out when 'split'
        finalEndDeletion = finalEndDeletion + 1
      end
    }


    tempFinalDeletion = @processedXMLString[finalEndDeletion..(@processedXMLString.length - 1)].index ">" 

    if nil == tempFinalDeletion
      tempFinalDeletion = 0
    end

    finalEndDeletion = finalEndDeletion + tempFinalDeletion

    @processedXMLString.slice!(finalStartDeletion..finalEndDeletion)

  end

end

